package com.api.gbhg.luoao.la.dao;


import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.huangwm.dao.YuSuanTiaoZhengDao;
import com.api.gbhg.luoao.la.util.Allutils;
import com.api.gbhg.luoao.la.util.DaoUtil;
import com.customization.assets.util.DateTools;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.neethi.All;
import org.springframework.orm.ojb.OjbAccessor;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.soa.workflow.request.*;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.api.gbhg.lbj.tool.Tools.*;

public class BudgetModifyDao {
    private static Logger logger = Logger.getLogger("gbhg");
    public static BaseBean baseBean = new BaseBean();
    private static YuSuanTiaoZhengDao dao = new YuSuanTiaoZhengDao();
    private static DateTools dateTools = new DateTools();

    private static String YJ;//科目级别
    private static String XJ;//科目下级


    //部门预算经济分类调整
    public static Map<String, Object> getDepartmentBudgetadjustment(RecordSetTrans recordSetTrans,
                                                                    String requestid, String dzxm, String type) {

        List<Map<String, Object>> list = getmxb(requestid);//获取细化经济分类
        boolean result = false;
        Map<String, Object> objectMap = new LinkedHashMap<>();
        objectMap.put("result", result);

        try {

            //获取原有的部门经济分类数据
            List<Map<String, Object>> sectoralBudgetlist = getSectoralBudgetlist(dzxm);
            if (list.size() < sectoralBudgetlist.size()) {
                objectMap.put("mg", "调整条数不能减少");
                return objectMap;
            }

            List<Map<String, Object>> yyxzlist = new ArrayList<>();
            //调整金额
            logger.info("开始调整金额--经济分类间调整");
            for (Map<String, Object> map : list) {
                //经济分类间调整
                if (map.get("dzfx").toString().equals("1")) {

                    for (int i = 0; i < sectoralBudgetlist.size(); i++) {
                        Map<String, Object> map1 = sectoralBudgetlist.get(i);
                        if (map.get("dzz").equals(map1.get("bmyszcjjfl"))) {

                            //调整至对应的部门经济分类
                            map1.put("hjwy", add(map1.get("hjwy").toString(), map.get("dzje").toString()));//加总金额
                            map1.put("syed", add(map1.get("syed").toString(), map.get("dzje").toString()));//加剩余金额
                            map1.put("dyzfjjfl", map.get("dzfx"));
                            sectoralBudgetlist.remove(i);
                            sectoralBudgetlist.add(i, map1);

                            //扣减自身的额度
                            for (int k = 0; k < sectoralBudgetlist.size(); k++) {
                                Map<String, Object> maps = sectoralBudgetlist.get(k);
                                if (map.get("bmyszcjjfl").equals(maps.get("bmyszcjjfl"))) {
                                    //自身的额度扣减调整
                                    maps.put("hjwy", subtract(maps.get("hjwy").toString(), map.get("dzje").toString()));//减总金额
                                    maps.put("syed", subtract(maps.get("syed").toString(), map.get("dzje").toString()));//减剩余金额
                                    sectoralBudgetlist.remove(k);
                                    sectoralBudgetlist.add(k, maps);
                                    break;
                                }
                            }
                            break;
                        }

                    }

                    //从已有经济分类新增
                } else if (map.get("dzfx").toString().equals("3")) {
                    //自身增加额度
                    map.put("dzfx", map.get("dzfx"));
                    yyxzlist.add(map);
                    //扣减对应额度
                    for (int p = 0; p < sectoralBudgetlist.size(); p++) {
                        Map<String, Object> maps = sectoralBudgetlist.get(p);
                        if (map.get("dzz").equals(maps.get("bmyszcjjfl"))) {
                            //扣减对应的额度扣减调整
                            maps.put("hjwy", subtract(maps.get("hjwy").toString(), map.get("dzje").toString()));//减总金额
                            maps.put("syed", subtract(maps.get("syed").toString(), map.get("dzje").toString()));//减剩余金额
                            sectoralBudgetlist.remove(p);
                            sectoralBudgetlist.add(p, maps);
                            break;
                        }
                    }
                //huangwm 获取预算减少下的细化经济分类数据
                }else if (map.get("dzfx").toString().equals("4")) {
                    //扣减对应额度
                    for (int p = 0; p < sectoralBudgetlist.size(); p++) {
                        Map<String, Object> maps = sectoralBudgetlist.get(p);
                        if (map.get("bmyszcjjfl").equals(maps.get("bmyszcjjfl"))) {
                            //扣减对应的额度扣减调整
                            maps.put("hjwy", subtract(maps.get("hjwy").toString(), map.get("dzje").toString()));//减总金额
                            maps.put("syed", subtract(maps.get("syed").toString(), map.get("dzje").toString()));//减剩余金额
                            sectoralBudgetlist.remove(p);
                            sectoralBudgetlist.add(p, maps);
                            break;
                        }

                    }

                }

            }

            logger.info("已有经济分类新增合并 之前的所有数据扣除结果" + yyxzlist + "/n" + sectoralBudgetlist);

            //处理 已有经济分类新增 相同部门经济分类的数据合并
            List<Map<String, Object>> interim = new ArrayList<>();

            List<String> bmyszcjjfllist = new ArrayList<>();
            for (Map<String, Object> map : yyxzlist) {
                bmyszcjjfllist.add(map.get("bmyszcjjfl").toString());
            }

            List<String> collects = bmyszcjjfllist.stream().distinct().collect(Collectors.toList());
            for (String s : collects) {

                List<Map<String, Object>> collect = yyxzlist.stream().filter(x -> x.get("bmyszcjjfl").equals(s)).collect(Collectors.toList());
                if (collect.size() > 1) {

                    BigDecimal ze = new BigDecimal("0.00");
                    for (Map<String, Object> stringObjectMap : collect) {
                        ze = ze.add(new BigDecimal(stringObjectMap.get("hjwy").toString()));
                    }
                    Map<String, Object> map = collect.get(0);
                    map.put("hjwy", ze);
                    interim.add(map);
                } else {
                    interim.add(collect.get(0));
                }

            }


            logger.info("处理 已有经济分类新增 相同部门经济分类的数据合并" + interim);

            yyxzlist = interim;

            //处理 已有经济分类新增 和原来的相同的数据合并
            for (int i = 0; i < yyxzlist.size(); i++) {

                Map<String, Object> map = yyxzlist.get(i);
                for (int k = 0; k < sectoralBudgetlist.size(); k++) {

                    Map<String, Object> maps = sectoralBudgetlist.get(k);
                    if (map.get("bmyszcjjfl").equals(maps.get("bmyszcjjfl"))) {

                        maps.put("hjwy", add(map.get("hjwy").toString(), maps.get("hjwy").toString()));
                        maps.put("syed", add(map.get("hjwy").toString(), maps.get("syed").toString()));
                        sectoralBudgetlist.remove(k);
                        sectoralBudgetlist.add(k, maps);
                        yyxzlist.remove(i);
                        break;
                    }
                }
            }


            //经济分类调整
            for (Map<String, Object> map : sectoralBudgetlist) {

                BigDecimal b1 = new BigDecimal(map.get("syed").toString());
                if (b1.compareTo(BigDecimal.ZERO) == -1) {
                    objectMap.put("mg", map.get("bmyszcjjfl") + "剩余金额不够调整");
                    return objectMap;
                }

            }

            //已有新增经济分类
            for (Map<String, Object> map : yyxzlist) {

                BigDecimal b1 = new BigDecimal(map.get("hjwy").toString());
                if (b1.compareTo(BigDecimal.ZERO) == -1) {
                    objectMap.put("mg", map.get("bmyszcjjfl") + "金额不能是负数");
                    recordSetTrans.rollback();
                    return objectMap;
                }

            }


            // type等于1 就是判断扣减金额是否够用
            if (type.equals("1")) {
                objectMap.put("result", true);
                return objectMap;
            }


            List<Map<String, Object>> pp = new ArrayList<>();
            pp.addAll(sectoralBudgetlist);
            pp.addAll(yyxzlist);

            logger.info("调整后的数据" + pp);

            //返回同步版本 数据
            if (type.equals("2")) {
                objectMap.put("result", true);
                objectMap.put("xfjjfmList", pp);
                return objectMap;
            }

            //循环更新经费表
            for (Map<String, Object> map : sectoralBudgetlist) {
                jingfeiUpdate(recordSetTrans, map.get("syed").toString()
                        , map.get("hjwy").toString(), map.get("id").toString());
            }

            //循环插入新经费表
            for (Map<String, Object> map : yyxzlist) {
                jingfeiInsert(recordSetTrans, dzxm
                        , map.get("bmyszcjjfl").toString(),
                        map.get("dyzfjjfl").toString(), map.get("hjwy").toString());
            }


            result = true;
            recordSetTrans.commit();
        } catch (Exception e) {
            recordSetTrans.rollback();
            objectMap.put("mg", "代码错误" + e.toString());
            return objectMap;

        }
        objectMap.put("result", result);
        return objectMap;
    }


    /**
     * 预算追加
     *
     * @param recordSetTrans 事物sql
     * @param formid         主表id
     * @param billid
     * @param sbdw           申报单位
     * @return
     */
    public static Map<String, Object> getBudgetSupplement(RecordSetTrans recordSetTrans, int formid, int billid, String sbdw) {
        boolean jude = false;
        Map<String, Object> objectMap = new LinkedHashMap<>();
        objectMap.put("result", jude);
        try {
            // 查询所属独立核酸单位下是否有机动经费
            String jdjfSql = "select lcjlsqjlid,xmmin,ysnd,lcbj,sydw,jbr from uf_jingfei_version where ysnd=? and dlhsdw=( \n" +
                    "select case when exists (select dlhsdw from uf_dlhsdwgl where dlhsdw=?)\n" +
                    " then (select dlhsdw from uf_dlhsdwgl where dlhsdw=?)\n" +
                    "  else (select supsubcomid from hrmsubcompany where id=?) end\n" +
                    ") and lcbj=19";
            // 获取机动经费数据
            Map<String, Object> mapBySql = DaoUtil.getMapBySql(jdjfSql, dateTools.getCurrentYear(), sbdw, sbdw, sbdw);
            // 判断是否又机动经费数据
            if (mapBySql != null && mapBySql.size() > 0) {

                String lcjlsqjlid = Util.null2String(mapBySql.get("lcjlsqjlid")); // 机动经费id

                String JJFL = dao.getJjfl_DtTablesNoZSXM(formid);//获取细化经济分类明细表名称
                //细化经济分类明细表集合
                List<Map<String, Object>> jjflList = DaoUtil.getListMapBySql("select * from " + JJFL + " where mainid=" + billid);
                for (Map<String, Object> itme : jjflList) {
                    Map<String, Object> bmyszcjjfl = DaoUtil.getMapBySql("select id,ybx,syed from uf_jingfei where lcjlid=? and bmzcjjfl=?"
                            , lcjlsqjlid, Util.null2String(itme.get("bmyszcjjfl")));
                    Map<String, Object> bySql = DaoUtil.getMapBySql("select yszje from uf_jingfei_version where lcjlsqjlid=?", lcjlsqjlid);
                    // 判读机动经费中是否存在 不存在则插入，存在则修改
                    if (bmyszcjjfl != null && bmyszcjjfl.size() > 0) {
                        String dzje = Util.null2String(itme.get("hjwy"));
                        String id = Util.null2String(bmyszcjjfl.get("id")); // 细化经济分类明细id
                        String ybx = Util.null2String(bmyszcjjfl.get("ybx")); // 已报销
                        String syed = Util.null2String(bmyszcjjfl.get("syed")); // 剩余额度
                        BigDecimal newSyed = subtract(syed, dzje); // 新剩余额度
                        if (newSyed.compareTo(BigDecimal.ZERO) >= 0) {
                            String updateSql = "update uf_jingfei set ybx = ?,syed = ? where id = ?";
                            jude = recordSetTrans.executeUpdate(updateSql, add(dzje, ybx).toString(), newSyed.toString(), id);
                        } else {
                            recordSetTrans.rollback();
                            objectMap.put("result", false);
                            objectMap.put("mg", "机动经费中部门预算支出经济分类:"+itme.get("bmyszcjjfl")+"额度不足！");
                            return objectMap;
                        }

                        BigDecimal zje = subtract(Util.null2String(bySql.get("yszje")), dzje); // 项目总金额
                        if (zje.compareTo(BigDecimal.ZERO) >= 0) {
                            String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                            jude = recordSetTrans.executeUpdate(sql, zje.toString(), lcjlsqjlid);
                        } else {
                            recordSetTrans.rollback();
                            objectMap.put("result", false);
                            objectMap.put("mg", "项目总金额不足！");
                            return objectMap;
                        }
                    } else {
                        recordSetTrans.rollback();
                        objectMap.put("mg", "机动经费中不存在 细化经济分类："+Util.null2String(itme.get("bmyszcjjfl"))+"");
                        return objectMap;
                    }
                }
                int number = 0;

                List<String> HPQD = dao.getHpqd_DtTablesNoZSXM(formid);//获取核批渠道明细表名称
                //当年预算明细表集合
                List<Map<String, Object>> dnysList = DaoUtil.getListMapBySql("select * from " + HPQD.get(0) + " where mainid=" + billid);
                for (Map<String, Object> itme : dnysList) {
                    Map<String, Object> bySql = DaoUtil.getMapBySql("select yszje from uf_jingfei_version where lcjlsqjlid=?", lcjlsqjlid);
                    StringBuilder sb = new StringBuilder();
                    sb.append("select id,syed from uf_jingfeilaiyuan where yslylx = '当年预算核批' and lcid=  " + lcjlsqjlid);

                    String srlx = Util.null2String(itme.get("srlx"));//收入类型
                    if (!srlx.isEmpty()) {
                        //0和1分别代表财政拨款和非财政拨款
                        if (srlx.equals("1")) {
                            sb.append(" and srlx = 1 ");
                            String zcqd = Util.null2String(itme.get("zcqd"));
                            if (zcqd.equals("5") || zcqd.equals("8")) {
                                sb.append(" and zcqd = "+zcqd+" and dfczbkxm = " + Util.null2String(itme.get("dfczbkxmdnys")));
                            } else {
                                sb.append(" and zcqd = " + zcqd);
                            }
                        } else {
                            sb.append(" and srlx = 0 and zyczbksr = '" + Util.null2String(itme.get("zyczbksr"))+"'");
                        }
                        Map<String, Object> jflyMap = DaoUtil.getMapBySql(sb.toString());
                        // 如果存在经费来源表中则进行修改
                        if (jflyMap != null && jflyMap.size() > 0) {
                            String hpje = Util.null2String(itme.get("hpje")); // 核批金额
                            String syed = Util.null2String(jflyMap.get("syed")); // 剩余额度
                            String id = Util.null2String(jflyMap.get("id"));
                            BigDecimal newSyed = subtract(syed, hpje); // 新剩余额度
                            if (newSyed.compareTo(BigDecimal.ZERO) >= 0) {
                                String updateSql = "update uf_jingfeilaiyuan set syed = ?  where id = ? ";
                                jude = recordSetTrans.executeUpdate(updateSql, newSyed.toString(), id);
                            } else {
                                recordSetTrans.rollback();
                                objectMap.put("result", false);
                                objectMap.put("mg",  "第"+(++number)+"行经费来源在机动经费中剩余额度不足！");
                                return objectMap;
                            }


                            BigDecimal zje = subtract(Util.null2String(bySql.get("yszje")), hpje); // 项目总金额
                            if (zje.compareTo(BigDecimal.ZERO) >= 0) {
                                String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                                jude = recordSetTrans.executeUpdate(sql, zje.toString(), lcjlsqjlid);
                            } else {
                                recordSetTrans.rollback();
                                objectMap.put("result", false);
                                objectMap.put("mg", "项目总金额不足！");
                                return objectMap;
                            }
                        } else {
                            // 不存在禁止提交
                            recordSetTrans.rollback();
                            objectMap.put("mg", "机动经费中不存在 当年预算第"+(++number)+"行核批渠道来源");
                            return objectMap;
                        }
                    } else {
                        recordSetTrans.rollback();
                        objectMap.put("mg", "当前预算收入预算为空！");
                        return objectMap;
                    }
                }

                number = 0;
                //上年结转明细表集合
                List<Map<String, Object>> snjzList = DaoUtil.getListMapBySql("select * from " + HPQD.get(1) + " where mainid=" + billid);
                for (Map<String, Object> itme : snjzList) {
                    Map<String, Object> bySql = DaoUtil.getMapBySql("select yszje from uf_jingfei_version where lcjlsqjlid=?", lcjlsqjlid);
                    StringBuilder sb = new StringBuilder();
                    sb.append("select id,syed,je from uf_jingfeilaiyuan where yslylx = '上年结转核批' and lcid=  " + lcjlsqjlid);

                    String srlx = Util.null2String(itme.get("srlx"));//收入预算
                    if (!srlx.isEmpty()) {
                        //0和1分别代表财政拨款和非财政拨款
                        if (srlx.equals("1")) {
                            sb.append(" and srlx = 1 ");
                            String zcqd = Util.null2String(itme.get("zcqd"));
                            if (zcqd.equals("5") || zcqd.equals("8")) {
                                sb.append(" and zcqd = "+zcqd+" and dfczbkxm = " + Util.null2String(itme.get("dfczbkxmsnjz")));
                            } else {
                                sb.append(" and zcqd = " + zcqd);
                            }
                        } else {
                            sb.append(" and srlx = 0 and zyczbksr = '" + Util.null2String(itme.get("zyczbksr"))+"'");
                        }
                        Map<String, Object> jflyMap = DaoUtil.getMapBySql(sb.toString());
                        // 如果存在经费来源表中则进行修改
                        if (jflyMap != null && jflyMap.size() > 0) {
                            String hpje = Util.null2String(itme.get("hpje")); // 核批金额
                            String syed = Util.null2String(jflyMap.get("syed")); // 剩余额度
                            String id = Util.null2String(jflyMap.get("id"));
                            BigDecimal newSyed = subtract(syed, hpje); // 新剩余额度
                            if (newSyed.compareTo(BigDecimal.ZERO) >= 0) {
                                String updateSql = "update uf_jingfeilaiyuan set syed = ?  where id = ? ";
                                jude = recordSetTrans.executeUpdate(updateSql, newSyed.toString(), id);
                            } else {
                                recordSetTrans.rollback();
                                objectMap.put("result", false);
                                objectMap.put("mg", "第"+(++number)+"行经费来源在机动经费中剩余额度不足！");
                                return objectMap;
                            }

                            BigDecimal zje = subtract(Util.null2String(bySql.get("yszje")), hpje); // 项目总金额
                            if (zje.compareTo(BigDecimal.ZERO) >= 0) {
                                String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                                jude = recordSetTrans.executeUpdate(sql, zje.toString(), lcjlsqjlid);
                            } else {
                                recordSetTrans.rollback();
                                objectMap.put("result", false);
                                objectMap.put("mg", "项目总金额不足！");
                                return objectMap;
                            }
                        } else {
                            // 不存在禁止提交
                            recordSetTrans.rollback();
                            objectMap.put("mg", "机动经费中不存在 上年结转第"+(++number)+"行核批渠道来源");
                            return objectMap;
                        }
                    } else {
                        recordSetTrans.rollback();
                        objectMap.put("mg", "上年结转收入预算为空！");
                        return objectMap;
                    }
                }

                if (jude) {
                    recordSetTrans.commit();
                    objectMap.put("result", jude);
                    return objectMap;
                } else {
                    recordSetTrans.rollback();
                    objectMap.put("mg", "数据插入异常");
                    return objectMap;
                }
            } else {
                objectMap.put("mg", "机动经费不存在！");
                return objectMap;
            }
        } catch (Exception e) {
            recordSetTrans.rollback();
            objectMap.put("mg", "代码错误" + e.toString());
            return objectMap;
        }
    }

    /**
     *经费额度信息插入
     * @param param
     * @return
     */
    public static boolean insertBudgeLines(Map<String,String> param,RecordSetTrans rst){
        String sql="insert into uf_jingfei(requestId,lcbj,lcjlid,bmzcjjfl,zfjjfl,nd,ze,syed,dj,sbdw,xmmc,modedatamodifier,modedatamodifydatetime,gkdw,sydw)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        logger.info("经费额度信息插入"+sql);
        try {
            return rst.executeUpdate(sql,
                    param.getOrDefault("requestId",""),
                    param.getOrDefault("lcbj",""),
                    param.getOrDefault("lcjlid",""),
                    param.getOrDefault("bmzcjjfl",""),
                    param.getOrDefault("zfjjfl",""),
                    param.getOrDefault("nd",""),
                    param.getOrDefault("ze",""),
                    param.getOrDefault("syed",""),
                    param.getOrDefault("dj",""),
                    param.getOrDefault("sbdw",""),
                    param.getOrDefault("xmmc",""),
                    param.getOrDefault("modedatamodifier",""),
                    param.getOrDefault("modedatamodifydatetime",""),
                    param.getOrDefault("gkdw",""),
                    param.getOrDefault("sydw","")
            );
        } catch (Exception e) {
            e.printStackTrace();
            rst.rollback();
            //return rst;
        }
        //rts.commit();
        //return true;
        return false;
    }

    /**
     * 预算减少
     *
     * @param recordSetTrans 事物sql
     * @param formid         主表id
     * @param billid
     * @param requestid
     * @param sbdw           申报单位
     * @param dzxm           调整项目
     * @return
     */
    public static Map<String, Object> getBudgetReduce(RecordSetTrans recordSetTrans, int formid, int billid
            , String requestid, String sbdw, String dzxm) {
        boolean jude = false;
        Map<String, Object> objectMap = new LinkedHashMap<>();
        objectMap.put("result", jude);

        try {
            String jdjfSql = "select lcjlsqjlid,xmmin,ysnd,lcbj,sydw,jbr from uf_jingfei_version where ysnd=? and dlhsdw=( \n" +
                    "select case when exists (select dlhsdw from uf_dlhsdwgl where dlhsdw=?)\n" +
                    " then (select dlhsdw from uf_dlhsdwgl where dlhsdw=?)\n" +
                    "  else (select supsubcomid from hrmsubcompany where id=?) end\n" +
                    ") and lcbj=19";
            // 获取机动经费数据
            Map<String, Object> mapBySql = DaoUtil.getMapBySql(jdjfSql, dateTools.getCurrentYear(), sbdw, sbdw, sbdw);
            // 判断是否又机动经费数据
            if (mapBySql != null && mapBySql.size() > 0) {
                YJ = baseBean.getPropValue("Verification", "yj");
                XJ = baseBean.getPropValue("Verification", "xj");
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);//当前年份
                int year1 = calendar.get(Calendar.YEAR);//上年年份
                String lcjlsqjlid = Util.null2String(mapBySql.get("lcjlsqjlid")); // 机动经费id
                String ysnd = Util.null2String(mapBySql.get("ysnd")); // 预算年度
                String JJFL = dao.getJjfl_DtTablesNoZSXM(formid);//获取细化经济分类明细表名称
                //细化经济分类明细表集合 开始
                List<Map<String, Object>> jjflList = DaoUtil.getListMapBySql("select * from " + JJFL + " where mainid=" + billid);
                for (Map<String, Object> itme : jjflList) {
                    //判断是否是预算减少
                    String dzfx = Util.null2String(itme.get("dzfx"));
                    // 判断明细表中 调整方向是否是预算减少
                    if (dzfx.equals("4")) {
                        // 调整机动经费预算 ↓
                        Map<String, Object> bmyszcjjfl = DaoUtil.getMapBySql("select id,ze,syed from uf_jingfei where lcjlid=? and bmzcjjfl=?"
                                , lcjlsqjlid, Util.null2String(itme.get("bmyszcjjfl")));
                        Map<String, Object> bySql = DaoUtil.getMapBySql("select yszje from uf_jingfei_version where lcjlsqjlid=?", lcjlsqjlid);
                        // 判读机动经费中是否存在 不存在则插入，存在则修改
                        if (bmyszcjjfl != null && bmyszcjjfl.size() > 0) {
                            String dzje = Util.null2String(itme.get("dzje"));//调整金额
                            jude = jingfeiUpdate(recordSetTrans
                                    , add(dzje, Util.null2String(bmyszcjjfl.get("syed"))).toString()
                                    , add(dzje, Util.null2String(bmyszcjjfl.get("ze"))).toString()
                                    , Util.null2String(bmyszcjjfl.get("id")));
                            BigDecimal zje = add(Util.null2String(bySql.get("yszje")), dzje); // 项目总金额
                            String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                            jude = recordSetTrans.executeUpdate(sql, zje.toString(), lcjlsqjlid);
                        } else {
                            Map<String, String> map = new HashMap<>();
                            map.put("requestId", requestid);
                            String lcbj = Util.null2String(mapBySql.get("lcbj"));
                            map.put("lcbj", lcbj);
                            map.put("lcjlid", lcjlsqjlid);
                            map.put("nd", ysnd);//预算年度
                            String hjwy = Util.null2String(itme.get("hjwy"));
                            map.put("ze", hjwy);
                            map.put("bmzcjjfl", Util.null2String(itme.get("bmyszcjjfl")));
                            map.put("zfjjfl", Util.null2String(itme.get("dyzfjjfl")));
                            map.put("gkdw", "");
                            String sydw = Util.null2String(mapBySql.get("sydw"));
                            map.put("sydw", sydw);
                            String syed = "0.00";
                            if (hjwy.equals("")) {
                                map.put("ze", "0.00");
                                syed = "0.00";
                            } else {
                                syed = hjwy;
                            }
                            map.put("syed", syed);
                            map.put("dj", "0.00");
                            map.put("sbdw", sbdw);
                            String xmmin = Util.null2String(mapBySql.get("xmmin"));
                            map.put("xmmc", xmmin);
                            String jbr = Util.null2String(mapBySql.get("jbr"));
                            map.put("modedatamodifier", jbr);
                            map.put("modedatamodifydatetime", TimeUtil.getCurrentTimeString());//当前日期时间
                            logger.info("经费入库：" + JSONObject.toJSONString(map));
                            jude = insertBudgeLines(map,recordSetTrans);

                            BigDecimal zje = add(Util.null2String(bySql.get("yszje")), syed); // 项目总金额
                            String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                            jude = recordSetTrans.executeUpdate(sql, zje.toString(), lcjlsqjlid);
                        }
                        // 调整机动预算 ↑

                        // 修改调整项目 ↓
                        Map<String, Object> newBmyszcjjfl = DaoUtil.getMapBySql("select id,ze,syed from uf_jingfei where lcjlid=? and bmzcjjfl=?"
                                , dzxm, Util.null2String(itme.get("bmyszcjjfl")));
                        Map<String, Object> newBySql = DaoUtil.getMapBySql("select yszje from uf_jingfei_version where lcjlsqjlid=?", dzxm);
                        // 如果不存在则不允许提交
                        if (newBmyszcjjfl != null && newBmyszcjjfl.size() > 0) {
                            String dzje = Util.null2String(itme.get("dzje"));
                            BigDecimal syed = subtract(Util.null2String(newBmyszcjjfl.get("syed")), dzje);
                            if (syed.compareTo(BigDecimal.ZERO) < 0) {
                                recordSetTrans.rollback();
                                objectMap.put("result", false);
                                objectMap.put("mg", "调整项目细化经济分类:"+Util.null2String(itme.get("bmyszcjjfl"))+"中剩余额度不足！");
                                return objectMap;
                            }
                            BigDecimal ze = subtract(Util.null2String(newBmyszcjjfl.get("ze")), dzje);
                            if (ze.compareTo(BigDecimal.ZERO) < 0) {
                                recordSetTrans.rollback();
                                objectMap.put("result", false);
                                objectMap.put("mg", "调整项目细化经济分类:"+Util.null2String(itme.get("bmyszcjjfl"))+"中总额不足！");
                                return objectMap;
                            }
                            jude = jingfeiUpdate(recordSetTrans
                                    , syed.toString()
                                    , ze.toString()
                                    , Util.null2String(newBmyszcjjfl.get("id")));
                            BigDecimal zje = subtract(Util.null2String(newBySql.get("yszje")), dzje); // 项目总金额
                            if (zje.compareTo(BigDecimal.ZERO) >= 0) {
                                String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                                jude = recordSetTrans.executeUpdate(sql, zje.toString(), dzxm);
                            } else {
                                recordSetTrans.rollback();
                                objectMap.put("result", false);
                                objectMap.put("mg", "项目总金额不足！");
                                return objectMap;
                            }
                        } else {
                            recordSetTrans.rollback();
                            objectMap.put("result", false);
                            objectMap.put("mg", "部门预算支出经济分类:"+Util.null2String(itme.get("bmyszcjjfl"))+"不存在于机动经费!");
                            return objectMap;
                        }
                        // 修改调整项目 ↑
                    }
                }
                //细化经济分类明细表集合 结束

                int number = 0;
                List<String> HPQD = dao.getHpqd_DtTablesNoZSXM(formid);//获取核批渠道明细表名称
                //当年预算明细表集合
                List<Map<String, Object>> dnysList = DaoUtil.getListMapBySql("select * from " + HPQD.get(0) + " where mainid=" + billid);
                for (Map<String, Object> itme : dnysList) {
                    Map<String, Object> bySql = DaoUtil.getMapBySql("select yszje from uf_jingfei_version where lcjlsqjlid=?", lcjlsqjlid);

                    String srlx = Util.null2String(itme.get("srlx"));//收入类型
                    if (!srlx.isEmpty()) {
                        // 调整机动经费预算 ↓
                        StringBuilder sb = new StringBuilder();
                        sb.append("select id,syed,je from uf_jingfeilaiyuan where yslylx = '当年预算核批' and lcid=  " + lcjlsqjlid);
                        //0和1分别代表财政拨款和非财政拨款
                        if (srlx.equals("1")) {
                            sb.append(" and srlx = 1 ");
                            String zcqd = Util.null2String(itme.get("zcqd"));
                            if (zcqd.equals("5") || zcqd.equals("8")) {
                                sb.append(" and zcqd = "+zcqd+" and dfczbkxm = " + Util.null2String(itme.get("dfczbkxmdnys")));
                            } else {
                                sb.append(" and zcqd = " + zcqd);
                            }
                        } else {
                            sb.append(" and srlx = 0 and zyczbksr = '" + Util.null2String(itme.get("zyczbksr"))+"'");
                        }
                        Map<String, Object> jflyMap = DaoUtil.getMapBySql(sb.toString());
                        // 如果存在经费来源表中则进行修改
                        if (jflyMap != null && jflyMap.size() > 0) {
                            //String hpje = Util.null2String(itme.get("hpje"));
                            String ysjs = Util.null2String(itme.get("ysjs"));//核批来源明细预算减少金额
                            try {
                                jude = recordSetTrans.executeUpdate("update uf_jingfeilaiyuan set syed = ?,je =?  where id = ? "
                                        , add(ysjs, Util.null2String(jflyMap.get("syed"))).toString()
                                        , add(ysjs, Util.null2String(jflyMap.get("je"))).toString()
                                        , Util.null2String(jflyMap.get("id")));

                                BigDecimal zje = add(Util.null2String(bySql.get("yszje")), ysjs); // 项目总金额

                                String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                                jude = recordSetTrans.executeUpdate(sql, zje.toString(), lcjlsqjlid);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            int sum1 = 0;
                            int sum2 = 0;
                            List<Object> listParams = new ArrayList<>();
                            //支出渠道
                            String zcqd = Util.null2String(itme.get("zcqd"));
                            //功能科目_新
                            String gnkm = "";
                            //中央财政拨款项目
                            String zyczbksr = Util.null2String(itme.get("zyczbksr"));
                            //地方财政拨款项目上年结转
                            String dfczbkxmdnys = Util.null2String(itme.get("dfczbkxmdnys"));
                            if (srlx.equals("0")) {//0：中央
                                if (StringUtils.isNotBlank(zyczbksr)) {
                                    String[] sta = zyczbksr.split("_");//当年结转核批下划线分割
                                    sum1 = Integer.parseInt(sta[0]);//9为科目级，10为科目下级
                                    sum2 = Integer.parseInt(sta[1]);//表id
                                    if (sum1 == Integer.parseInt(YJ)) {
                                        gnkm = getSubjectsGnkm(sum2);
                                    } else if (sum1 == Integer.parseInt(XJ)) {
                                        gnkm = getSubjectsGnkm(getSubjectsGnkm2(sum2));
                                    }
                                }
                            }
                            if (srlx.equals("1")) {//1:非中央
                                RecordSet rs1 = theCentral(zcqd, year1, sbdw);
                                rs1.next();
                                gnkm = rs1.getString("gnkm");
                            }
                            //核批金额
                            //String hpje = Util.null2String(itme.get("hpje"));
                            String ysjs = Util.null2String(itme.get("ysjs"));//核批来源明细预算减少金额
                            listParams.add(sbdw); // 上报单位
                            listParams.add(ysnd); // 预算年度
                            listParams.add(zcqd); // 来源类型
                            listParams.add(gnkm); // 功能科目
                            listParams.add(ysjs); // 金额
                            listParams.add(ysjs); // 剩余额度
                            listParams.add(0); // 冻结金额
                            listParams.add("当年预算核批"); // 预算来源类型
                            listParams.add(srlx); // 收入类型
                            listParams.add(dfczbkxmdnys); // 地方财政拨款项目当年预算
                            listParams.add(zyczbksr); // 中央财政拨款收入
                            listParams.add(lcjlsqjlid);
                            String sqlInsert = "INSERT into uf_jingfeilaiyuan(sbdw,nd,zcqd,gnkm,je,syed,djje,yslylx,srlx,dfczbkxm,zyczbksr,lcid)\n" +
                                    "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
                            logger.info(requestid + "：当年预算入库sql：" + sqlInsert);
                            logger.info(requestid + "：当年预算入库值：" + listParams);
                            jude = recordSetTrans.executeUpdate(sqlInsert, listParams);

                            BigDecimal zje = add(Util.null2String(bySql.get("yszje")), ysjs); // 项目总金额

                            String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                            recordSetTrans.executeUpdate(sql, zje.toString(), lcjlsqjlid);
                        }
                        // 调整预算 ↑

                        // 调整项目 ↓
                        Map<String, Object> newBySql = DaoUtil.getMapBySql("select yszje from uf_jingfei_version where lcjlsqjlid=?", dzxm);
                        StringBuilder dzxmSb = new StringBuilder();
                        dzxmSb.append("select id,syed,je from uf_jingfeilaiyuan where yslylx = '当年预算核批' and lcid=  " + dzxm);
                        //0和1分别代表财政拨款和非财政拨款
                        if (srlx.equals("1")) {
                            dzxmSb.append(" and srlx = 1 ");
                            String zcqd = Util.null2String(itme.get("zcqd"));
                            if (zcqd.equals("5") || zcqd.equals("8")) {
                                dzxmSb.append(" and zcqd = "+zcqd+" and dfczbkxm = " + Util.null2String(itme.get("dfczbkxmdnys")));
                            } else {
                                dzxmSb.append(" and zcqd = " + zcqd);
                            }
                        } else {
                            dzxmSb.append(" and srlx = 0 and zyczbksr = '" + Util.null2String(itme.get("zyczbksr"))+"'");
                        }
                        Map<String, Object> dzxmJflyMap = DaoUtil.getMapBySql(dzxmSb.toString());
                        if (dzxmJflyMap != null && dzxmJflyMap.size() > 0) {
                            //String hpje = Util.null2String(itme.get("hpje"));
                            String ysjs = Util.null2String(itme.get("ysjs"));//核批来源明细预算减少金额
                            try {
                                BigDecimal syed = subtract(Util.null2String(dzxmJflyMap.get("syed")), ysjs);
                                if (syed.compareTo(BigDecimal.ZERO) < 0) {
                                    recordSetTrans.rollback();
                                    objectMap.put("result", false);
                                    objectMap.put("mg", "调整项目当年预算来源明细中剩余额度不足！");
                                    return objectMap;
                                }
                                BigDecimal je = subtract(Util.null2String(dzxmJflyMap.get("je")), ysjs);
                                if (je.compareTo(BigDecimal.ZERO) < 0) {
                                    recordSetTrans.rollback();
                                    objectMap.put("result", false);
                                    objectMap.put("mg", "调整项目当年预算来源明细中金额不足！");
                                    return objectMap;
                                }
                                jude = recordSetTrans.executeUpdate("update uf_jingfeilaiyuan set syed = ?,je =?  where id = ? "
                                        , syed.toString()
                                        , je.toString()
                                        , Util.null2String(dzxmJflyMap.get("id")));

                                BigDecimal zje = subtract(Util.null2String(newBySql.get("yszje")), ysjs); // 项目总金额
                                if (zje.compareTo(BigDecimal.ZERO) >= 0) {
                                    String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                                    jude = recordSetTrans.executeUpdate(sql, zje.toString(), dzxm);
                                } else {
                                    recordSetTrans.rollback();
                                    objectMap.put("result", false);
                                    objectMap.put("mg", "项目总金额不足！");
                                    return objectMap;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            recordSetTrans.rollback();
                            objectMap.put("result", false);
                            objectMap.put("mg", "当年预算核批渠道第："+(++number)+"行未存在于机动经费！");
                            return objectMap;
                        }
                        // 调整项目 ↑

                    }
                }

                number = 0;
                //上年结转明细表集合
                List<Map<String, Object>> snjzList = DaoUtil.getListMapBySql("select * from " + HPQD.get(1) + " where mainid=" + billid);
                for (Map<String, Object> itme : snjzList) {
                    Map<String, Object> bySql = DaoUtil.getMapBySql("select yszje from uf_jingfei_version where lcjlsqjlid=?", lcjlsqjlid);
                    StringBuilder sb = new StringBuilder();
                    sb.append("select id,syed,je from uf_jingfeilaiyuan where yslylx = '上年结转核批' and lcid=  " + lcjlsqjlid);

                    String srlx = Util.null2String(itme.get("srlx"));//收入预算
                    if (!srlx.isEmpty()) {
                        // 调整机动经费预算 ↓
                        //0和1分别代表财政拨款和非财政拨款
                        if (srlx.equals("1")) {
                            sb.append(" and srlx = 1 ");
                            String zcqd = Util.null2String(itme.get("zcqd"));
                            if (zcqd.equals("5") || zcqd.equals("8")) {
                                sb.append(" and zcqd = "+zcqd+" and dfczbkxm = " + Util.null2String(itme.get("dfczbkxmsnjz")));
                            } else {
                                sb.append(" and zcqd = " + zcqd);
                            }
                        } else {
                            sb.append(" and srlx = 0 and zyczbksr = '" + Util.null2String(itme.get("zyczbksr"))+"'");
                        }
                        Map<String, Object> jflyMap = DaoUtil.getMapBySql(sb.toString());
                        // 如果存在经费来源表中则进行修改
                        if (jflyMap != null && jflyMap.size() > 0) {
                            //String hpje = Util.null2String(itme.get("hpje"));
                            String ysjs = Util.null2String(itme.get("ysjs"));//核批来源明细预算减少金额
                            try {
                                jude = recordSetTrans.executeUpdate("update uf_jingfeilaiyuan set syed = ?,je =?  where id = ? "
                                        , add(ysjs, Util.null2String(jflyMap.get("syed"))).toString()
                                        , add(ysjs, Util.null2String(jflyMap.get("je"))).toString()
                                        , Util.null2String(jflyMap.get("id")));

                                BigDecimal zje = add(Util.null2String(bySql.get("yszje")), ysjs); // 项目总金额

                                String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                                jude = recordSetTrans.executeUpdate(sql, zje.toString(), lcjlsqjlid);

                            } catch (Exception e) {
                                recordSetTrans.rollback();
                                objectMap.put("mg", "代码错误" + e.toString());
                                return objectMap;
                            }
                        } else {
                            int sum1 = 0;
                            int sum2 = 0;
                            List<Object> listParams = new ArrayList<>();
                            //支出渠道
                            String zcqd = Util.null2String(itme.get("zcqd"));
                            //功能科目_新
                            String gnkm = "";
                            //中央财政拨款项目
                            String zyczbksr = Util.null2String(itme.get("zyczbksr"));
                            //地方财政拨款项目上年结转
                            String dfczbkxmsnjz = Util.null2String(itme.get("dfczbkxmsnjz"));
                            if (srlx.equals("0")) {//0：中央
                                if (StringUtils.isNotBlank(zyczbksr)) {
                                    String[] sta = zyczbksr.split("_");//当年结转核批下划线分割
                                    sum1 = Integer.parseInt(sta[0]);//9为科目级，10为科目下级
                                    sum2 = Integer.parseInt(sta[1]);//表id
                                    if (sum1 == Integer.parseInt(YJ)) {
                                        gnkm = getSubjectsGnkm(sum2);
                                    } else if (sum1 == Integer.parseInt(XJ)) {
                                        gnkm = getSubjectsGnkm(getSubjectsGnkm2(sum2));
                                    }
                                }
                            }
                            if (srlx.equals("1")) {//1:非中央
                                RecordSet rs1 = theCentral(zcqd, year, sbdw);
                                rs1.next();
                                gnkm = rs1.getString("gnkm");
                            }
                            //核批金额
                            //String hpje = Util.null2String(itme.get("hpje"));
                            String ysjs = Util.null2String(itme.get("ysjs"));//核批来源明细预算减少金额
                            listParams.add(sbdw); // 上报单位
                            listParams.add(ysnd); // 预算年度
                            listParams.add(zcqd); // 来源类型
                            listParams.add(gnkm); // 功能科目
                            listParams.add(ysjs); // 金额
                            listParams.add(ysjs); // 剩余额度
                            listParams.add(0); // 冻结金额
                            listParams.add("上年结转核批"); // 预算来源类型
                            listParams.add(srlx); // 收入类型
                            listParams.add(dfczbkxmsnjz); // 地方财政拨款项目上年结转
                            listParams.add(zyczbksr); // 中央财政拨款收入
                            listParams.add(lcjlsqjlid);
                            String sqlINSERT = "INSERT into uf_jingfeilaiyuan(sbdw,nd,zcqd,gnkm,je,syed,djje,yslylx,srlx,dfczbkxm,zyczbksr,lcid)\n" +
                                    "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
                            logger.info(requestid + "：上年结转入库sql：" + sqlINSERT);
                            logger.info(requestid + "：上年结转入库值：" + listParams);
                            jude = recordSetTrans.executeUpdate(sqlINSERT, listParams);

                            BigDecimal zje = add(Util.null2String(bySql.get("yszje")), ysjs); // 项目总金额

                            String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                            jude = recordSetTrans.executeUpdate(sql, zje.toString(), lcjlsqjlid);

                        }
                        // 调整机动经费预算 ↑


                        // 调整项目 ↓
                        Map<String, Object> newBySql = DaoUtil.getMapBySql("select yszje from uf_jingfei_version where lcjlsqjlid=?", dzxm);
                        StringBuilder dzxmSb = new StringBuilder();
                        dzxmSb.append("select id,syed,je from uf_jingfeilaiyuan where yslylx = '上年结转核批' and lcid=  " + dzxm);
                        //0和1分别代表财政拨款和非财政拨款
                        if (srlx.equals("1")) {
                            dzxmSb.append(" and srlx = 1 ");
                            String zcqd = Util.null2String(itme.get("zcqd"));
                            if (zcqd.equals("5") || zcqd.equals("8")) {
                                dzxmSb.append(" and zcqd = "+zcqd+" and dfczbkxm = " + Util.null2String(itme.get("dfczbkxmsnjz")));
                            } else {
                                dzxmSb.append(" and zcqd = " + zcqd);
                            }
                        } else {
                            dzxmSb.append(" and srlx = 0 and zyczbksr = '" + Util.null2String(itme.get("zyczbksr"))+"'");
                        }
                        Map<String, Object> dzxmJflyMap = DaoUtil.getMapBySql(dzxmSb.toString());
                        if (dzxmJflyMap != null && dzxmJflyMap.size() > 0) {
                            //String hpje = Util.null2String(itme.get("hpje"));
                            String ysjs = Util.null2String(itme.get("ysjs"));
                            try {
                                BigDecimal syed = subtract(Util.null2String(dzxmJflyMap.get("syed")), ysjs);
                                if (syed.compareTo(BigDecimal.ZERO) < 0) {
                                    recordSetTrans.rollback();
                                    objectMap.put("result", false);
                                    objectMap.put("mg", "调整项目上年结转预算来源明细中剩余额度不足！");
                                    return objectMap;
                                }
                                BigDecimal je = subtract(Util.null2String(dzxmJflyMap.get("je")), ysjs);
                                if (je.compareTo(BigDecimal.ZERO) < 0) {
                                    recordSetTrans.rollback();
                                    objectMap.put("result", false);
                                    objectMap.put("mg", "调整项目上年机动经费不足结转预算来源明细中金额不足！");
                                    return objectMap;
                                }
                                jude = recordSetTrans.executeUpdate("update uf_jingfeilaiyuan set syed = ?,je =?  where id = ? "
                                        , syed.toString()
                                        , je.toString()
                                        , Util.null2String(dzxmJflyMap.get("id")));

                                BigDecimal zje = subtract(Util.null2String(newBySql.get("yszje")), ysjs); // 项目总金额
                                if (zje.compareTo(BigDecimal.ZERO) >= 0) {
                                    String sql = "update uf_jingfei_version set yszje = ? where lcjlsqjlid= ?";
                                    jude = recordSetTrans.executeUpdate(sql, zje.toString(), dzxm);
                                } else {
                                    recordSetTrans.rollback();
                                    objectMap.put("result", false);
                                    objectMap.put("mg", "项目总金额不足！");
                                    return objectMap;
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            recordSetTrans.rollback();
                            objectMap.put("result", false);
                            objectMap.put("mg", "上年结转核批渠道第："+(++number)+"行未存在于机动经费！");
                            return objectMap;
                        }
                        // 调整项目 ↑
                    }

                }

                if (jude) {
                    recordSetTrans.commit();
                    objectMap.put("result", jude);
                    return objectMap;
                } else {
                    recordSetTrans.rollback();
                    objectMap.put("mg", "数据插入异常");
                    return objectMap;
                }
            } else {
                objectMap.put("mg", "机动经费不存在！");
                return objectMap;
            }
        } catch (Exception e) {
            recordSetTrans.rollback();
            objectMap.put("mg", "代码错误" + e.toString());
            return objectMap;
        }
    }


    //金额相减
    public static BigDecimal subtract(String a, String b) {

        BigDecimal bigDecimal = new BigDecimal(a);
        BigDecimal bigDecimal1 = new BigDecimal(b);

        return bigDecimal.subtract(bigDecimal1);

    }


    //金额相加
    public static BigDecimal add(String a, String b) {

        BigDecimal bigDecimal = new BigDecimal(a);
        BigDecimal bigDecimal1 = new BigDecimal(b);


        return bigDecimal.add(bigDecimal1);

    }


    //获取上年结转
    public static List<Map<String, Object>> getLastYearBudget(String requestid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select srlx,zyczbksr,zcqd,dfczbkxmsnjz,hpje from formtable_main_996 t1,formtable_main_996_dt5 t2" +
                " where t1.id = t2.mainid  and requestid = " + requestid);

        List<Map<String, Object>> listmap = new ArrayList<>();
        while (rs.next()) {
            Map<String, Object> map = new HashMap<>();
            map.put("srlx", rs.getString("srlx"));
            map.put("zyczbksr", rs.getString("zyczbksr"));
            map.put("zcqd", rs.getString("zcqd"));
            map.put("dfczbkxmsnjz", rs.getString("dfczbkxmsnjz"));
            map.put("hpje", rs.getString("hpje"));
            listmap.add(map);
        }
        return listmap;
    }


    //获取当年结转
    public static List<Map<String, Object>> getThisYearBudget(String requestid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select srlx,zyczbksr,zcqd,dfczbkxmdnys,hpje from formtable_main_996 t1,formtable_main_996_dt6 t2" +
                " where t1.id = t2.mainid  and requestid = " + requestid);

        List<Map<String, Object>> listmap = new ArrayList<>();
        while (rs.next()) {
            Map<String, Object> map = new HashMap<>();
            map.put("srlx", rs.getString("srlx"));
            map.put("zyczbksr", rs.getString("zyczbksr"));
            map.put("zcqd", rs.getString("zcqd"));
            map.put("dfczbkxmdnys", rs.getString("dfczbkxmdnys"));
            map.put("hpje", rs.getString("hpje"));
            listmap.add(map);
        }
        return listmap;
    }


    //调整开关 以及 跨类开关
    public static Map<String, Object> getSwitch() {
        logger.info("获取调整开关~~");
        String adjust = "0";
        String adjustClass = "0";
        Map<String, Object> map = new HashMap<>();
        map.put("adjust", adjust);
        map.put("adjustClass", adjustClass);

        Map controlSwichData = Switch();
        if (controlSwichData.isEmpty()) return map;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date kdzkssj = sdf.parse(controlSwichData.get("kdzkssj").toString());//开始调整时间
            Date kdzjssj = sdf.parse(controlSwichData.get("kdzjssj").toString());//结束调整时间
            Date date = sdf.parse(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));//当前时间

            //判断是否在时间之内
            if ((date.after(kdzkssj) && date.before(kdzjssj)) ||
                    (date.getTime() == kdzkssj.getTime() || date.getTime() == kdzjssj.getTime())) {
                adjust = "1";
                adjustClass = "1";

            }

        } catch (ParseException e) {
            map.put("adjust", adjust);
            map.put("adjustClass", adjustClass);
            return map;
        }


        //调整开关是否打开
        if (controlSwichData.get("sfyxysdz").equals("1")) {
            adjust = "1";
        }

        //跨类开关是否打开
        if (controlSwichData.get("sfyxkldz").equals("1")) {
            adjustClass = "1";
        }


        map.put("adjust", adjust);
        map.put("adjustClass", adjustClass);
        return map;
    }


    public static Map<String, Object> Switch() {
        RecordSet rs = new RecordSet();
        String sql = "select sfyxysdz,sfyxkldz,kdzkssj,kdzjssj from uf_gnkg";
        rs.executeQuery(sql);
        Map<String, Object> map = new HashMap<>();
        if (rs.next()) {
            map.put("sfyxysdz", rs.getString("sfyxysdz")); //是否允预算调整
            map.put("sfyxkldz", rs.getString("sfyxkldz")); //是否允许跨类调整
            map.put("kdzkssj", rs.getString("kdzkssj"));   //可调整开始时间
            map.put("kdzjssj", rs.getString("kdzjssj"));   //可调整结束时间
        }
        return map;
    }


    //获取对应的明细表数据
    public static List<Map<String, Object>> getDetailTable(Row[] rows) {

        List<Map<String, Object>> list = new ArrayList<>();
        // 当前明细表的所有数据，按行存储
        for (Row row : rows) {
            // 每行数据再按列存储
            Cell[] cells = row.getCell();
            Map<String, Object> map = new HashMap<>();
            for (Cell cell : cells) {
                map.put(cell.getName(), cell.getValue());
            }
            list.add(map);
        }

        return list;
    }


    //更新经费表里面对应部门经济分类 剩余额度
    public static boolean jingfeiUpdate(RecordSetTrans recordSetTrans, String syed, String ze, String id) {
        boolean b = false;
        try {
            b = recordSetTrans.executeUpdate("update uf_jingfei set syed = ?,ze = ? where id = ?",
                    syed, ze, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }

    //新增 经费数据
    public static boolean jingfeiInsert(RecordSetTrans recordSetTrans, String requestid,
                                        String bmzcfjjfl, String zfjjfl, String ze) {
        //获取原有数据
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from uf_jingfei where lcjlid=?", requestid);
        rs.next();
        Map<String, String> map = new HashMap<>();
        map.put("sydw", rs.getString("sydw"));
        map.put("lcbj", rs.getString("lcbj"));
        map.put("nd", rs.getString("nd"));
        map.put("sbdw", rs.getString("sbdw"));
        map.put("xmmc", rs.getString("xmmc"));
        map.put("modedatamodifier", rs.getString("modedatamodifier"));

        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        boolean b = false;
        try {
            b = recordSetTrans.executeQuery("INSERT INTO uf_jingfei(requestId,sydw,lcbj,lcjlid,bmzcjjfl,zfjjfl,nd,ze," +
                            "syed,dj,sbdw,xmmc,gkdw,ybx,modedatamodifier,modedatamodifydatetime)  " +
                            "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", requestid, map.get("sydw"),
                    map.get("lcbj"), requestid, bmzcfjjfl, zfjjfl, map.get("nd"), ze, ze, "0.00", map.get("sbdw"), map.get("xmmc"),
                    "0", "0.00", map.get("modedatamodifier"), date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }


    //更新跨类 跨款
    public static boolean classPaymentUpdate(RecordSetTrans recordSetTrans,
                                             int classId, int paymentId,
                                             String tableName, String requestid) {

        boolean b = false;
        try {
            b = recordSetTrans.executeUpdate("update " + tableName + " set jjfldzsfkl = ?,jjfldzsfkk=? where requestid =?"
                    , classId, paymentId, requestid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;

    }

    //获取部门经济分类
    public static List<Map<String, Object>> getSectoralBudgetlist(String requestid) {
        List<Map<String, Object>> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id,lcjlid,bmzcjjfl,zfjjfl,ze,syed from uf_jingfei where lcjlid =" + requestid);
        while (rs.next()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", rs.getString("id"));
            map.put("lcjlid", rs.getString("lcjlid"));
            map.put("bmyszcjjfl", rs.getString("bmzcjjfl"));
            map.put("dyzfjjfl", rs.getString("zfjjfl"));
            map.put("hjwy", rs.getString("ze"));
            map.put("syed", rs.getString("syed"));
            list.add(map);
        }

        return list;
    }


    public static Map<String, Object> getProperty(RequestInfo requestInfo) {

        Map<String, Object> map = new HashMap<>();
        //获取调整项目 和 调整类型
        String dzlx = "";
        String dzxm = "";
        String sbdw = "";
        String requestid = requestInfo.getRequestid();
        Property[] propertys = requestInfo.getMainTableInfo().getProperty();
        for (Property property : propertys) {

            if (property.getName().equals("dzlx")) {
                dzlx = property.getValue();
            }

            if (property.getName().equals("dzxm")) {
                dzxm = property.getValue();
            }
            if (property.getName().equals("szdlhsdw")) {
                sbdw = property.getValue();
            }

        }
        map.put("dzlx", dzlx);
        map.put("dzxm", dzxm);
        map.put("sbdw", sbdw);
        map.put("requestid", requestid);

        return map;

    }


    public static List<Map<String, Object>> getmxb(String requestid) {

        //获取流程的所有明细表名
        List<String> detailtableName = Allutils.getDetailtableName(Allutils.getFormid(Allutils.getworkfolwid(requestid)));
        Collections.reverse(detailtableName);

        List<Map<String, Object>> list = new ArrayList<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from " + detailtableName.get(3) + " where mainid=" + Allutils.getMainid(requestid));
        while (rs.next()) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", rs.getString("id"));
            map.put("xzlybs", rs.getString("xzlybs"));
            map.put("lb", rs.getString("lb"));
            map.put("mxx", rs.getString("mxx"));
            map.put("hjwy", rs.getString("hjwy"));
            map.put("czbkwy", rs.getString("czbkwy"));
            map.put("jzzjwy", rs.getString("jzzjwy"));
            map.put("bmyszcjjfl", rs.getString("bmyszcjjfl"));
            map.put("dyzfjjfl", rs.getString("dyzfjjfl"));
            map.put("dzje", rs.getString("dzje"));
            map.put("dzz", rs.getString("dzz"));
            map.put("dzfx", rs.getString("dzfx"));
            list.add(map);

        }
        return list;
    }


    //获取对应政府经济分类
    public static List<String> getzfjjfl(String bmjjfl) {
        List<String> list = new ArrayList<>();

        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery("select zfjjzcflkm from uf_jjzcflkmk where kdm=" + bmjjfl);
        while (recordSet.next()) {
            list.add(recordSet.getString("zfjjzcflkm"));
        }

        return list;

    }
}
